package com.zyr.model;

import com.zyr.constraint.BusinessEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author 张雨润
 * @date 2024/7/26 上午9:06
 * @Description 统一返回类型
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;
    private static final Integer SUCCESS_CODE = 200;
    private static final Integer ERROR_CODE = 500;

    /**
     * 无参成功返回
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS_CODE)
        .setMsg("操作成功");
        return result;
    }
    public static <T> Result<T> success(String msg) {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS_CODE)
                .setMsg(msg);
        return result;
    }

    /**
     * 成功返回 数据
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS_CODE)
                .setMsg("操作成功")
                .setData(data);
        return result;
    }

    /**
     * 错误状态返回 状态码，错误信息
     */
    public static <T> Result<T> error(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code)
                .setMsg(msg)
                .setData(null);
        return result;
    }

    // 枚举常量 状态异常 返回
    public static <T> Result<T> error(BusinessEnum businessEnum){
        return new Result()
                .setCode(businessEnum.getCode())
                .setMsg(businessEnum.getDesc())
                .setData(null);
    }
}
